package com.shareyi.baseproject.kernel.adapter.spi.conf;

import com.shareyi.baseproject.kernel.common.spi.conf.Settings;
import com.shareyi.baseproject.kernel.common.utils.MyStringUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 配置map bean
 *
 * @author zhangshibin
 * @date 2021/9/1
 */
public class ConfigMapBean implements Settings {

    /**
     * 配置map
     */
    protected Map<String, Object> config;


    public Map<String, Object> getConfig() {
        return config;
    }

    public void setConfig(Map<String, Object> config) {
        this.config = config;
    }

    @Override
    public Integer getInteger(String key) {
        return MapUtils.getInteger(config, key);
    }

    @Override
    public Integer getInteger(String key, Integer defaultValue) {
        return MapUtils.getInteger(config, key, defaultValue);
    }

    @Override
    public String getString(String key) {
        return MapUtils.getString(config, key);
    }

    @Override
    public String getString(String key, String defaultValue) {
        return MapUtils.getString(config, key, defaultValue);
    }

    @Override
    public Double getDouble(String key) {
        return MapUtils.getDouble(config, key);
    }

    @Override
    public Double getDouble(String key, Double defaultValue) {
        return MapUtils.getDouble(config, key, defaultValue);
    }

    @Override
    public Long getLong(String key) {
        return MapUtils.getLong(config, key);
    }

    @Override
    public Long getLong(String key, Long defaultValue) {
        return MapUtils.getLong(config, key, defaultValue);
    }

    @Override
    public Boolean getBoolean(String key) {
        return MapUtils.getBoolean(config, key);
    }

    @Override
    public Boolean getBoolean(String key, Boolean defaultValue) {
        return MapUtils.getBoolean(config, key, defaultValue);
    }

    @Override
    public List<String> getWithSeparator(String key, String separator) {
        return getWithSeparator(key, separator, Collections.emptyList());
    }

    @Override
    public List<String> getWithSeparator(String key, String separator, List<String> defList) {
        String value = getString(key);
        if (StringUtils.isEmpty(value)) {
            return defList;
        }
        return MyStringUtils.splitToList(value, separator);
    }

    @Override
    public <T> T getObject(String key, Class<T> clazz) {
        return getObject(key, clazz, null);
    }


    @Override
    public <T> T getObject(String key, Class<T> clazz, T defValue) {
        return (T) MapUtils.getObject(config, key, defValue);
    }
}
